#include<bits/stdc++.h>
using namespace std;
int book[20005];
int main()
{
	int t,flag,temp,i,j,same,judge2,judge1;
	scanf("%d",&t);getchar();
	while(t--)
	{
		flag=judge1=judge2=same=0;
		memset(book,0,sizeof(book));
		for(i=1;i<=4;i++)
		{
			scanf("%d",&temp);
			book[temp]++;
		}
		for(i=1;i<=20000;i++)
		{
			if(book[i]==0)	continue;
			if(book[i]==3||book[i]==4)
			{
				flag=3;
				same=i;
				break;
			}
			if(book[i]==2)
			{
				if(same==0)
					judge2+=(i*2);
				same=i;
				flag=2;
			}	
			if(book[i]==1)
			{
				judge1+=i;
			}
		}
		if(flag==1)	continue;
		else
		{
			
			if(same==judge1||same==judge2||flag==3)
				printf("%d\n",same);
			else
				printf("-1\n");
		}
	}
	return 0;
}

